import {
  Component,
  ViewChild,
  Injector,
  Output,
  EventEmitter,
  ElementRef,
} from "@angular/core";
import { ModalDirective } from "ngx-bootstrap";
import { AppComponentBase } from "@shared/common/app-component-base";

import * as moment from "moment";
import { finalize } from "rxjs/operators";
import {
  MainTaskServiceProxy,
  TaskDto,
  MainTaskGoProductionDto,
  NameValueDtoOfNameValueDto,
  PipelineServiceProxy,
  SideTaskServiceProxy,
  NameValueDto,
  TeamGroupTaskServiceProxy,
} from "@shared/service-proxies/service-proxies";
import { LazyLoadEvent } from "primeng/api";

@Component({
  selector: "boundGangModal",
  templateUrl: "./bound-gang-modal.component.html",
})
export class BoundGangModalComponent extends AppComponentBase {
  @ViewChild("actionModal") modal: ModalDirective;
  @ViewChild("dueDatePicker") dueDatePicker: ElementRef;
  @ViewChild("tmplCombobox") tmplCombobox: ElementRef;

  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;
  dueDate = new Date();
  event?: LazyLoadEvent;
  id: string;
  mainTeamGroup: string;
  remark: string;
  teamGroupName = new NameValueDto();
  teamGroups = new Array<NameValueDto>();

  constructor(
    injector: Injector,
    private _teamGroupTaskService: TeamGroupTaskServiceProxy
  ) {
    super(injector);
  }

  show(id: string, mainTeamGroup: string, event?: LazyLoadEvent): void {
    this.id = id;
    this.event = event;
    this.active = true;
    this._teamGroupTaskService
      .getGangByOrgName(mainTeamGroup)
      .subscribe((result) => {
        this.teamGroups = result;
        this.modal.show();
      });
  }

  save(): void {
    this.saving = true;
    var gtime = moment(this.dueDate).add(8, "hours");
    this._teamGroupTaskService
      .boundGangZhang(this.id, this.teamGroupName.name, gtime)
      .pipe(
        finalize(() => {
          this.saving = false;
        })
      )
      .subscribe((result) => {
        this.notify.info("绑定成功");
        this.close();
        this.modalSave.emit(this.event);
      });
  }

  close(): void {
    this.saving = false;
    this.modal.hide();
  }
}
